﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：有載 检测50个 有BOM
    /// </summary>
    public class R020 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;

            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //sheet.GetRow(1).GetCell(headColumnsIndex + 33).SetCellValue(head.LcMax);

                    //1.Customer:
                    if (sheet.GetRow(2).GetCell(headColumnsIndex + 2) != null)
                        sheet.GetRow(2).GetCell(headColumnsIndex + 2).SetCellValue(head.CustomerCode);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 9).SetCellValue(head.LotNo);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 16).SetCellValue(head.Series);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 22).SetCellValue(head.Cap);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 24).SetCellValue(head.Voltage);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 29).SetCellValue(head.Size);

                    //sheet.GetRow(2).GetCell(headColumnsIndex + 32).SetCellValue(head.Rubber);




                    sheet.GetRow(3).GetCell(headColumnsIndex + 3).SetCellValue(head.FoilP);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 9).SetCellValue(head.FoilN);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 16).SetCellValue(head.Eps);
                    if (sheet.GetRow(3).GetCell(headColumnsIndex + 23) == null) 
                        sheet.GetRow(3).CreateCell(headColumnsIndex + 23);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 23).SetCellValue(head.Paste);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 29).SetCellValue(head.DateCode);
                    //sheet.GetRow(3).GetCell(headColumnsIndex + 32).SetCellValue(head.FoilpSupplier);
                    //sheet.GetRow(3).GetCell(headColumnsIndex + 35).SetCellValue(head.Pn);
                    //sheet.GetRow(3).GetCell(headColumnsIndex + 36).SetCellValue(head.CustomerCode);

                    sheet.GetRow(4).GetCell(headColumnsIndex + 2).SetCellValue(head.TestVoltage);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 15).SetCellValue(head.TestTemp);
                    sheet.GetRow(4).GetCell(headColumnsIndex + 22).SetCellValue(head.TestTotalTime);

                    if (head.RippleCurrent > 0)
                    {
                        //RC:		mA/	120Hz
                        if (sheet.GetRow(4).GetCell(headColumnsIndex + 7) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 7);
                        sheet.GetRow(4).GetCell(headColumnsIndex + 7).SetCellValue("RC:");
                        if (sheet.GetRow(4).GetCell(headColumnsIndex + 8) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 8);
                        sheet.GetRow(4).GetCell(headColumnsIndex + 8).SetCellValue(head.RippleCurrent);
                        if (sheet.GetRow(4).GetCell(headColumnsIndex + 9) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 9);
                        sheet.GetRow(4).GetCell(headColumnsIndex + 9).SetCellValue("mA /");
                        if (sheet.GetRow(4).GetCell(headColumnsIndex + 10) == null) sheet.GetRow(4).CreateCell(headColumnsIndex + 10);
                        sheet.GetRow(4).GetCell(headColumnsIndex + 10).SetCellValue(head.TestFreq);

                        //Load  life (DC+RC) Test Report
                        sheet.GetRow(1).GetCell(headColumnsIndex + 0).SetCellValue("Load  life (DC+RC) Test Report");
                    }

                    //sheet.GetRow(5).GetCell(headColumnsIndex + 49).SetCellValue(head.SampleDate.ToString("yyyy/M/d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 50).SetCellValue(head.TestDate.ToString("yyyy-M-d"));
                    //sheet.GetRow(5).GetCell(headColumnsIndex + 51).SetCellValue(head.TestDate.ToString("yyyy-M-d"));



                    sheet.GetRow(275).GetCell(headColumnsIndex + 6).SetCellValue(head.Remark);


                    //Tested by:王燕
                    sheet.GetRow(282).GetCell(headColumnsIndex + 24).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }


                headColumnsIndex += 29;
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 8;
            int currentDtlColumnIndex = 1;
            int currentHeadRowIndex = 5;
            int dtlColumns = 5;
            int dtlRowsCount = 54;

            string RT = "RT:";
            string RH = "RH:";

            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 30) break;

                try 
                {
                    try
                    {
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex).SetCellValue(
                            head.Dtls[i].TestTime.ToString() + "hr");
                        sheet.GetRow(currentHeadRowIndex).GetCell(currentDtlColumnIndex + 2).SetCellValue(
                            head.Dtls[i].TestDate.ToString("yyyy/M/d"));

                        //RT: 
                        sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex).SetCellValue(RT);
                        sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 1).SetCellValue(
                            head.Dtls[i].GetTemp());
                        sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 2).SetCellValue(RH);
                        sheet.GetRow(currentHeadRowIndex + 1).GetCell(currentDtlColumnIndex + 3).SetCellValue(
                            head.Dtls[i].GetHumidity());

                        //CAP(uF)	^C/C(%)	DF(%)	△DF(%)	LC(uA)
                        if (i > 0)
                        {
                            sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex).SetCellValue("CAP(uF)");
                            sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex + 1).SetCellValue("^C/C(%)");
                            sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex + 2).SetCellValue("tanδ");
                            sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex + 3).SetCellValue("△tanδ");
                            sheet.GetRow(currentHeadRowIndex + 2).GetCell(currentDtlColumnIndex + 4).SetCellValue("LC(uA)");
                        }

                        //if (sheet.GetRow(70).GetCell(i + 1) == null) sheet.GetRow(70).CreateCell(i + 1);
                        //sheet.GetRow(70).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime.ToString() + "hr");
                        //if (sheet.GetRow(91).GetCell(i + 1) == null) sheet.GetRow(91).CreateCell(i + 1);
                        //sheet.GetRow(91).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime.ToString() + "hr");
                        //if (sheet.GetRow(112).GetCell(i + 1) == null) sheet.GetRow(112).CreateCell(i + 1);
                        //sheet.GetRow(112).GetCell(i + 1).SetCellValue(head.Dtls[i].TestTime.ToString() + "hr");
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex);
                    }

                    #region data
                    int cIndex = currentDataColumnIndex;
                    var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                    if (caps.Any())
                    {
                        var carpss = caps.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = carpss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                        //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                        for (int k = 0; k < carpss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (carpss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    if (i > 0) cIndex += 2;
                    else cIndex++;
                    var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                    if (dfs.Any())
                    {
                        var dfss = dfs.ToList();
                        for (int k = 0; k < dfss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (dfss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                        }
                    }
                    if (i == 0) cIndex++;

                    if (i > 0) cIndex += 2;
                    else cIndex++;
                    var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    #endregion data

                    currentDtlColumnIndex += dtlColumns;
                    currentDataColumnIndex += dtlColumns;

                    if (i > 0 && (i + 1) % 6 == 0)
                    {
                        currentDtlColumnIndex = 1;
                        currentDataColumnIndex = 1;
                        currentHeadRowIndex += dtlRowsCount;
                        dataRowIndex += dtlRowsCount;
                    }
                }
                catch(Exception ex)
                {
                    Logger.LogError(ex);
                }
            }

            int headCount = (int)(Math.Ceiling(head.Dtls.Count / 6d));
            int firstDataRow = 5;
            if (headCount < 5)
            {
                int lastDataRowIndex = firstDataRow + dtlRowsCount * 5;
                int firstHideRowIndex = firstDataRow + headCount * dtlRowsCount;
                for (int i = firstHideRowIndex; i < lastDataRowIndex; i++)
                {
                    sheet.GetRow(i).ZeroHeight = true;
                }
            }

            #endregion


            sheet.ForceFormulaRecalculation = true;
        }
    }
}
